package com.fr.report.io;

import com.fr.base.FRFont;
import com.fr.base.Painter;
import com.fr.base.Style;
import com.fr.base.core.GraphHelper;
import com.fr.report.CellElement;
import com.fr.report.FloatElement;
import com.fr.report.PageSet;
import com.fr.report.PageSetChain;
import com.fr.report.Report;
import com.fr.report.ReportHF;
import com.fr.report.ReportPage;
import com.fr.report.ReportSettings;
import com.fr.report.ResultWorkBook;
import com.fr.report.cellElement.CellGUIAttr;
import com.fr.report.cellElement.Formula;
import com.fr.report.cellElement.core.ResultSubReport;
import com.fr.report.core.DynamicValueList;
import com.fr.report.core.PaintUtils;
import com.fr.report.core.ReportHelper;
import com.fr.report.core.ReportUtils;
import com.fr.report.io.pdf.MyFontMapper;
import com.fr.report.painter.PDF2Painter;
import com.fr.third.com.lowagie.text.Document;
import com.fr.third.com.lowagie.text.Element;
import com.fr.third.com.lowagie.text.Font;
import com.fr.third.com.lowagie.text.Paragraph;
import com.fr.third.com.lowagie.text.Rectangle;
import com.fr.third.com.lowagie.text.pdf.PdfContentByte;
import com.fr.third.com.lowagie.text.pdf.PdfPCell;
import com.fr.third.com.lowagie.text.pdf.PdfPTable;
import com.fr.third.com.lowagie.text.pdf.PdfWriter;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Rectangle2D.Double;
import java.awt.image.BufferedImage;
import java.io.OutputStream;
import java.util.Iterator;

public class PDFExporter2 extends PDFExporter
{
  private boolean isPrint;
  private float unitScale;

  public PDFExporter2()
  {
    this(false);
  }

  public PDFExporter2(boolean paramBoolean)
  {
    this.isPrint = false;
    this.unitScale = 1.0F;
    this.isPrint = paramBoolean;
  }

  public void export(OutputStream paramOutputStream, ResultWorkBook paramResultWorkBook)
    throws Exception
  {
    PageSet localPageSet = paramResultWorkBook.generateReportPageSet(ReportUtils.getPaperSettingListFromWorkBook(paramResultWorkBook)).getTraversingCase();
    export(paramOutputStream, localPageSet);
    localPageSet.release();
  }

  public void export(OutputStream paramOutputStream, PageSet paramPageSet)
    throws Exception
  {
    Document localDocument = new Document();
    PdfWriter localPdfWriter = PdfWriter.getInstance(localDocument, paramOutputStream);
    localDocument.open();
    prepareFontMapper();
    for (int i = 0; i < paramPageSet.size(); ++i)
    {
      ReportPage localReportPage = paramPageSet.getPage(i);
      if (localReportPage == null)
        break;
      this.unitScale = 0.75F;
      float f1 = localReportPage.getPageWidth() * this.unitScale;
      float f2 = localReportPage.getPageHeight() * this.unitScale;
      float f3 = localReportPage.getMarginLeft() * this.unitScale;
      float f4 = localReportPage.getMarginRight() * this.unitScale;
      float f5 = localReportPage.getMarginTop() * this.unitScale;
      float f6 = localReportPage.getMarginBottom() * this.unitScale;
      ReportSettings localReportSettings = localReportPage.getReportSettings();
      float f7 = ReportUtils.getHeaderHeight(localReportSettings) * this.unitScale;
      float f8 = ReportUtils.getFooterHeight(localReportSettings) * this.unitScale;
      localDocument.setPageSize(new Rectangle(f1, f2));
      localDocument.setMargins(f3, f4, f5, f6);
      localDocument.newPage();
      PdfContentByte localPdfContentByte1 = localPdfWriter.getDirectContentUnder();
      Graphics2D localGraphics2D = localPdfContentByte1.createGraphics(f1, f2, prepareFontMapper());
      if (!(this.isPrint))
        localGraphics2D.scale(this.unitScale, this.unitScale);
      if (localReportSettings.isLonelyPage())
      {
        localObject1 = localReportPage.getSourceReport();
        DynamicValueList localDynamicValueList3 = ReportHelper.getColumnWidthList((Report)localObject1);
        localObject2 = ReportHelper.getRowHeightList((Report)localObject1);
        int l = ((Report)localObject1).getColumnCount();
        int i1 = ((Report)localObject1).getRowCount();
        int i2 = 0;
        int i3 = 0;
        for (int i4 = 0; i4 < l; ++i4)
          i2 += localDynamicValueList3.get(i4);
        for (i4 = 0; i4 < i1; ++i4)
          i3 += ((DynamicValueList)localObject2).get(i4);
        double d1 = localReportPage.getPageWidth() / (localReportPage.getMarginLeft() + localReportPage.getMarginRight() + i2);
        double d2 = localReportPage.getPageHeight() / (i3 + localReportPage.getMarginTop() + localReportPage.getMarginBottom());
        d1 = (d1 < 1.0D) ? d1 : 1.0D;
        d2 = (d2 < 1.0D) ? d2 : 1.0D;
        localGraphics2D.scale(d1, d2);
      }
      new PDF2Painter(localReportPage, localGraphics2D, this.isPrint).convert();
      localGraphics2D.dispose();
      int j = localReportPage.getColumnCount();
      int k = localReportPage.getRowCount();
      DynamicValueList localDynamicValueList1 = ReportHelper.createColumnWidthList(localReportPage);
      DynamicValueList localDynamicValueList2 = ReportHelper.createRowHeightList(localReportPage);
      Object localObject1 = sumDynamicValueList2Float(j, localDynamicValueList1);
      float f9 = localObject1[(localObject1.length - 1)];
      Object localObject2 = sumDynamicValueList2Float(k, localDynamicValueList2);
      float f10 = localObject2[(localObject2.length - 1)];
      ReportHF localReportHF = localReportPage.getHeader();
      if ((f7 > 0.0F) && (localReportHF != null))
        localDocument.add(createImageFromHFElement(localReportHF, localReportPage, f1 - f3 - f4, f7, this.isPrint));
      float[] arrayOfFloat = DynamicValueList2FloatArray(j, localDynamicValueList1);
      PdfPTable localPdfPTable = new PdfPTable(arrayOfFloat);
      localPdfPTable.setSpacingBefore(0.0F);
      localPdfPTable.setSpacingAfter(f2 - f10 - f7 - f8 - f5 - f6);
      localPdfPTable.setTotalWidth(f9);
      localPdfPTable.setLockedWidth(true);
      localPdfPTable.setHorizontalAlignment(0);
      Color localColor = Color.white;
      localPdfPTable.getDefaultCell().setBackgroundColor(localColor);
      localPdfPTable.getDefaultCell().setBorderWidth(0.0F);
      Iterator localIterator1 = localReportPage.cellIterator();
      int i5 = -1;
      int i6 = -1;
      int i7 = 0;
      int i8 = 0;
      byte[][] arrayOfByte = new byte[localReportPage.getRowCount()][localReportPage.getColumnCount()];
      while (localIterator1.hasNext())
      {
        localObject3 = (CellElement)localIterator1.next();
        i7 = ((CellElement)localObject3).getRow();
        i8 = ((CellElement)localObject3).getColumn();
        if ((i7 != 0) || (i8 != 0))
          fillBlankCell(localPdfPTable, arrayOfByte, i5, i6, i7, i8, localDynamicValueList2);
        float f11 = f2 - f5 - f7 - localObject2[localObject3.getRow()];
        float f12 = f3 + localObject1[localObject3.getColumn()];
        localPdfPTable.addCell(createCellFromCellElement(localPdfWriter, f12, f11, (CellElement)localObject3, localDynamicValueList1, localDynamicValueList2, this.isPrint));
        for (int i9 = 0; i9 < ((CellElement)localObject3).getRowSpan(); ++i9)
          for (int i10 = 0; i10 < ((CellElement)localObject3).getColumnSpan(); ++i10)
            arrayOfByte[(i7 + i9)][(i8 + i10)] = 1;
        i5 = i7;
        i6 = i8;
      }
      fillBlankCell(localPdfPTable, arrayOfByte, i5, i6, -1, -1, localDynamicValueList2);
      localDocument.add(localPdfPTable);
      Object localObject3 = localReportPage.getFooter();
      if ((f8 > 0.0F) && (localObject3 != null))
        localDocument.add(createImageFromHFElement((ReportHF)localObject3, localReportPage, f1 - f3 - f4, f8, this.isPrint));
      Iterator localIterator2 = localReportPage.floatIterator();
      while (true)
      {
        float f13;
        float f14;
        java.awt.Image localImage;
        do
        {
          if (!(localIterator2.hasNext()))
            break label1151;
          FloatElement localFloatElement = (FloatElement)localIterator2.next();
          f13 = f3 + localObject1[localFloatElement.getColumn()] + localFloatElement.getLeftDistance() * this.unitScale;
          f14 = f2 - f5 - f7 - localObject2[localFloatElement.getRow()] - localFloatElement.getTopDistance() * this.unitScale;
          float f15 = (float)localFloatElement.getSize().getWidth() * this.unitScale;
          float f16 = (float)localFloatElement.getSize().getHeight() * this.unitScale;
          f14 -= f16;
          localImage = drawFloatImage(localFloatElement, (int)f15, (int)f16);
        }
        while (localImage == null);
        PdfContentByte localPdfContentByte2 = localPdfWriter.getDirectContent();
        com.fr.third.com.lowagie.text.Image localImage1 = com.fr.third.com.lowagie.text.Image.getInstance(localImage, null);
        localImage1.setAbsolutePosition(f13, f14);
        localPdfContentByte2.addImage(localImage1);
      }
    }
    label1151: localDocument.close();
  }

  private void fillBlankCell(PdfPTable paramPdfPTable, byte[][] paramArrayOfByte, int paramInt1, int paramInt2, int paramInt3, int paramInt4, DynamicValueList paramDynamicValueList)
  {
    for (int i = (paramInt1 < 0) ? 0 : paramInt1; i < paramArrayOfByte.length; ++i)
      for (int j = (i == paramInt1) ? paramInt2 + 1 : 0; j < paramArrayOfByte[0].length; ++j)
      {
        if ((i == paramInt3) && (j == paramInt4))
          return;
        if (paramArrayOfByte[i][j] == 0)
        {
          paramPdfPTable.getDefaultCell().setFixedHeight(paramDynamicValueList.get(i) * this.unitScale);
          paramPdfPTable.addCell("");
          paramArrayOfByte[i][j] = 1;
        }
      }
  }

  private PdfPCell createCellFromCellElement(PdfWriter paramPdfWriter, float paramFloat1, float paramFloat2, CellElement paramCellElement, DynamicValueList paramDynamicValueList1, DynamicValueList paramDynamicValueList2, boolean paramBoolean)
    throws Exception
  {
    PdfPCell localPdfPCell = new PdfPCell();
    Style localStyle = paramCellElement.getStyle();
    Object localObject1 = paramCellElement.getValue();
    int i = paramDynamicValueList1.getRangeValue(paramCellElement.getColumn(), paramCellElement.getColumn() + paramCellElement.getColumnSpan());
    int j = paramDynamicValueList2.getRangeValue(paramCellElement.getRow(), paramCellElement.getRow() + paramCellElement.getRowSpan());
    float f1 = i * this.unitScale;
    float f2 = j * this.unitScale;
    i = (int)f1;
    j = (int)f2;
    localPdfPCell.setColspan(paramCellElement.getColumnSpan());
    localPdfPCell.setRowspan(paramCellElement.getRowSpan());
    boolean bool = false;
    CellGUIAttr localCellGUIAttr = paramCellElement.getCellGUIAttr();
    if (localCellGUIAttr == null)
      localCellGUIAttr = new CellGUIAttr();
    if ((!(localObject1 instanceof java.awt.Image)) && (((!(paramBoolean)) || (localCellGUIAttr.isPrintContent()))))
    {
      Object localObject2;
      Object localObject3;
      if ((!(localObject1 instanceof Painter)) && (!(localObject1 instanceof ResultSubReport)) && (!(localObject1 instanceof java.awt.Image)) && (checkWriteable(localStyle)))
      {
        if (localObject1 instanceof Formula)
          localObject1 = ((Formula)localObject1).getResult();
        localObject2 = PaintUtils.valueToText(localObject1, localStyle.getFormat());
        localObject3 = new Paragraph((String)localObject2, frFont2Font(localStyle.getFRFont()));
        if (localStyle.getRotation() % 90 == 0)
          localPdfPCell.setRotation(localStyle.getRotation());
        ((Paragraph)localObject3).setSpacingAfter(localStyle.getSpacingAfter() * this.unitScale);
        ((Paragraph)localObject3).setSpacingBefore(localStyle.getSpacingBefore() * this.unitScale);
        ((Paragraph)localObject3).setLeading(localStyle.getLineSpacing() * this.unitScale, 1.0F);
        localPdfPCell.addElement((Element)localObject3);
        bool = true;
      }
      if (!(bool))
      {
        localObject2 = GraphHelper.createBufferedImage((int)(i / this.unitScale), (int)(j / this.unitScale), 6);
        localObject3 = ((BufferedImage)localObject2).createGraphics();
        ((Graphics2D)localObject3).setPaint(Color.WHITE);
        if ((localObject1 instanceof Painter) || (localObject1 instanceof java.awt.Image))
        {
          PaintUtils.paintContent((Graphics2D)localObject3, localObject1, localStyle, (int)(i / this.unitScale), (int)(j / this.unitScale));
        }
        else
        {
          if (Color.BLACK.equals(localStyle.getFRFont().getForeground()))
            localStyle = localStyle.deriveFRFont(localStyle.getFRFont().applyForeground(new Color(1, 0, 0)));
          else if (Color.WHITE.equals(localStyle.getFRFont().getForeground()))
            localStyle = localStyle.deriveFRFont(localStyle.getFRFont().applyForeground(new Color(255, 255, 254)));
          PaintUtils.paintContent((Graphics2D)localObject3, localObject1, localStyle, (int)(i / this.unitScale), (int)(j / this.unitScale));
        }
        ((BufferedImage)localObject2).flush();
        ((Graphics2D)localObject3).dispose();
        localPdfPCell.setImage(com.fr.third.com.lowagie.text.Image.getInstance((java.awt.Image)localObject2, null));
      }
    }
    localPdfPCell.setFixedHeight(f2);
    applyCellStyle(localPdfPCell, localStyle, bool);
    return ((PdfPCell)(PdfPCell)localPdfPCell);
  }

  private boolean checkWriteable(Style paramStyle)
  {
    if (paramStyle.getVerticalText() == 1)
      return false;
    if ((paramStyle.getFRFont() != null) && (((paramStyle.getFRFont().isShadow()) || (paramStyle.getFRFont().isSubscript()) || (paramStyle.getFRFont().isSuperscript()))))
      return false;
    if (paramStyle.getTextStyle() != 0)
      return false;
    return (paramStyle.getRotation() % 90 == 0);
  }

  private void applyCellStyle(PdfPCell paramPdfPCell, Style paramStyle, boolean paramBoolean)
  {
    switch (paramStyle.getHorizontalAlignment())
    {
    case 0:
      paramPdfPCell.setHorizontalAlignment(1);
      break;
    case 4:
      paramPdfPCell.setHorizontalAlignment(2);
    }
    switch (paramStyle.getVerticalAlignment())
    {
    case 3:
      paramPdfPCell.setVerticalAlignment(6);
      break;
    case 0:
      paramPdfPCell.setVerticalAlignment(5);
    }
    paramPdfPCell.setPaddingLeft((GraphHelper.getLineStyleSize(paramStyle.getBorderLeft()) + ((paramBoolean) ? paramStyle.getPaddingLeft() : 0)) * this.unitScale);
    paramPdfPCell.setPaddingRight((GraphHelper.getLineStyleSize(paramStyle.getBorderRight()) + ((paramBoolean) ? paramStyle.getPaddingRight() : 0)) * this.unitScale);
    paramPdfPCell.setPaddingTop((GraphHelper.getLineStyleSize(paramStyle.getBorderTop()) + ((paramBoolean) ? paramStyle.getSpacingBefore() : 0)) * this.unitScale);
    paramPdfPCell.setPaddingBottom((GraphHelper.getLineStyleSize(paramStyle.getBorderBottom()) + ((paramBoolean) ? paramStyle.getSpacingAfter() : 0)) * this.unitScale);
    paramPdfPCell.setBorderWidth(0.0F);
  }

  private com.fr.third.com.lowagie.text.Image createImageFromHFElement(ReportHF paramReportHF, ReportPage paramReportPage, float paramFloat1, float paramFloat2, boolean paramBoolean)
    throws Exception
  {
    Rectangle2D.Double localDouble = new Rectangle2D.Double(0.0D, 0.0D, paramFloat1, paramFloat2);
    BufferedImage localBufferedImage = GraphHelper.createBufferedImage((int)paramFloat1, (int)paramFloat2, 2);
    Graphics2D localGraphics2D = localBufferedImage.createGraphics();
    localGraphics2D.setPaint(Color.white);
    GraphHelper.fill(localGraphics2D, localDouble);
    paramReportHF.paint(localGraphics2D, localDouble, (int)localDouble.getWidth(), (int)localDouble.getHeight(), paramReportPage.getCurrentPageNumber(), paramReportPage.getTotalPages(), paramBoolean);
    localBufferedImage.flush();
    localGraphics2D.dispose();
    return com.fr.third.com.lowagie.text.Image.getInstance(localBufferedImage, null);
  }

  private Font frFont2Font(FRFont paramFRFont)
  {
    int i;
    switch (paramFRFont.getStyle())
    {
    case 0:
      i = 0;
      break;
    case 1:
      i = 1;
      break;
    case 2:
      i = 2;
      break;
    case 3:
      i = 3;
      break;
    default:
      i = 0;
    }
    if (paramFRFont.isStrikethrough())
      i += 8;
    if (paramFRFont.getUnderline() != 0)
      i += 4;
    Font localFont = new Font(fontMapper.awtToPdf(paramFRFont), paramFRFont.getSize() * this.unitScale, i, paramFRFont.getForeground());
    return localFont;
  }

  private float[] DynamicValueList2FloatArray(int paramInt, DynamicValueList paramDynamicValueList)
  {
    float[] arrayOfFloat = new float[paramInt];
    for (int i = 0; i < arrayOfFloat.length; ++i)
      arrayOfFloat[i] = (paramDynamicValueList.get(i) * this.unitScale);
    return arrayOfFloat;
  }

  private float[] sumDynamicValueList2Float(int paramInt, DynamicValueList paramDynamicValueList)
  {
    int i = 0;
    float[] arrayOfFloat = new float[paramInt + 1];
    for (int j = 0; j < arrayOfFloat.length - 1; ++j)
    {
      i += paramDynamicValueList.get(j);
      arrayOfFloat[(j + 1)] = (i * this.unitScale);
    }
    return arrayOfFloat;
  }

  private java.awt.Image drawFloatImage(FloatElement paramFloatElement, int paramInt1, int paramInt2)
  {
    if ((paramFloatElement == null) || (paramInt1 == 0) || (paramInt2 == 0))
      return null;
    BufferedImage localBufferedImage = GraphHelper.createBufferedImage(paramInt1, paramInt2, 6);
    Graphics2D localGraphics2D = localBufferedImage.createGraphics();
    localGraphics2D.setPaint(Color.white);
    PaintUtils.paintFloatElement(localGraphics2D, paramFloatElement, paramInt1 - 1, paramInt2 - 1);
    localBufferedImage.flush();
    localGraphics2D.dispose();
    return localBufferedImage;
  }
}